<?php

class Controller_Video extends Stourweb_Controller{

    /**
     * 上传视频
     */
    public function action_upload_video(){
        try{
            $video=$_FILES['local_video'];
            if (empty($video['tmp_name'])) {
                throw new \Exception('请选择视频文件', 500);
            }
            $video_ext = ['mp4','mpeg','baiavi','navi','asf','mov','3gp','wmv','divx','xvid','rm','rmvb','flv','f4v'];
            $ext = pathinfo($video['name'])['extension'];
            if (!in_array($ext, $video_ext)) {
                throw new \Exception('错误的视频文件', 500);
            }
            if($video['size']>5242880){
                throw new \Exception('视频文件不能超过5M', 500);
            }
            $dir="/uploads/videos/{$ext}s/" . date('Y/md/');
            $path = BASEPATH  .$dir;
            if(!self::mkdirs( $path)) throw new \Exception('目录创建失败', 500);
            move_uploaded_file($video['tmp_name'],$path.DS.$video['name']);
            echo json_encode(['code'=>200,'msg'=>'上传成功','video'=>$dir.$video['name']]);
            exit();
        }catch (\Exception $e){
            echo json_encode(['status'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>'']);
            exit();
        }
    }

    /**
     * 上传封面
     */
    public function action_upload_pic(){
        try{
            $pic=$_FILES['pic'];
            if (empty($pic['tmp_name'])) {
                throw new \Exception('请选择视频封面', 500);
            }
            $pic_ext = ['jpg','png','gif','jpeg'];
            $ext = pathinfo($pic['name'])['extension'];
            if (!in_array($ext, $pic_ext)) {
                throw new \Exception('错误的视频封面', 500);
            }
            if($pic['size']>2097152){
                throw new \Exception('视频封面不能超过2M', 500);
            }
            $dir="/uploads/videos/{$ext}s/" . date('Y/md/');
            $path = BASEPATH  .$dir;
            if(!self::mkdirs( $path)) throw new \Exception('目录创建失败', 500);
            move_uploaded_file($pic['tmp_name'],$path.DS.$pic['name']);
            $pic_name = $dir.$pic['name'];
            echo json_encode(['code'=>200,'msg'=>'上传成功','pic'=>$pic_name]);
            exit();
        }catch (\Exception $e){
            echo json_encode(['status'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>'']);
            exit();
        }
    }

    public static function mkdirs($path) {
        if (!is_dir($path)) {
            self::mkdirs(dirname($path));
            mkdir($path,0777);
        }
        return is_dir($path);
    }

    /**
     * 下载视频
     * @throws Exception
     */
    public function action_download(){
        $url = $_POST['online_video'];
        $ext = pathinfo($url)['extension'];
        $save_dir=BASEPATH."/uploads/videos/{$ext}s/" . date('Y/md/');
        if(!self::mkdirs( $save_dir)) throw new \Exception('目录创建失败', 500);
        $file_name = time();
        $fileurl = $save_dir.$file_name.'.'.$ext;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $file = curl_exec($ch);
        $resource = fopen($fileurl, 'a');
        fwrite($resource, $file);
        fclose($resource);
        $errno = curl_errno($ch);
        curl_close($ch);
        $video_path=str_replace(BASEPATH,'',$fileurl);//去掉头
        $video_path=str_replace('\\','/',$video_path);
        if(empty($errno)) {
            echo json_encode(['code'=>200,'msg'=>'下载成功','video'=>$video_path]);
            exit();
        } else {
            echo json_encode(['code'=>500,'msg'=>'下载失败','video'=>$video_path]);
            exit();
        }

    }

}